package com.mmall.concurrency.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample6 {

    public static void main(String[] args) {

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 4, 60L
                , TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

        // 当执行的任务数量大于最大线程池数量的时候抛异常，也就是i必须小于等于4，这里写的5因此会抛异常
        // SynchronousQueue可以传参设定是否为公平锁
        for (int i = 0; i < 5; i++) {
            final int num = i;
            poolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(num);
                }
            });
        }

        poolExecutor.shutdown();

    }
}
